package buildcode.shop;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import buildcode.build.AbstractSingleTableCommandExecuter;
import buildcode.config.EntityExportConfig;
import buildcode.database.DataBaseTable;
import buildcode.language.Type;
import buildcode.language.es.EsUtil;

public class VueListPageCommandExecute extends AbstractSingleTableCommandExecuter {
    private String[] excludeSearchFields;
    private String[] excludeTableFields;

    public VueListPageCommandExecute() {
        super();
    }

    public VueListPageCommandExecute(String[] excludeSearchFields, String[] excludeTableFields) {
        super();
        this.excludeSearchFields = excludeSearchFields;
        this.excludeTableFields = excludeTableFields;
    }

    @Override
    public List<Type> execute(DataBaseTable base, EntityExportConfig config) {
        VueListPageEntity entity = new VueListPageEntity(StringUtils.firstNonBlank(config.getFileName(), base.getCamelCaseName() + config.getSuffix()), config.getPackageName());
        base.getColumns().stream().forEach(column -> {
            if (excludeSearchFields != null && excludeSearchFields.length > 0) {
                if (!ArrayUtils.contains(excludeSearchFields, column.getCamelCaseName())) {
                    entity.addSearchField(ShopUtil.buildField(column));
                }
                if (!ArrayUtils.contains(excludeTableFields, column.getCamelCaseName())) {
                    VueField vueField = ShopUtil.buildField(column);
                    if ("datetimerange".equals(vueField.getFrameType()) || "input".equals(vueField.getFrameType())) {
                        vueField.setFrameType("");
                    }
                    entity.addTableField(vueField);
                }
            }
        });

        return Arrays.asList(entity);
    }

    public String[] getExcludeSearchFields() {
        return excludeSearchFields;
    }

    public void setExcludeSearchFields(String[] excludeSearchFields) {
        this.excludeSearchFields = excludeSearchFields;
    }

    public String[] getExcludeTableFields() {
        return excludeTableFields;
    }

    public void setExcludeTableFields(String[] excludeTableFields) {
        this.excludeTableFields = excludeTableFields;
    }

}
